using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using BTV.Base;

namespace BTV.Bleb
{
    public class BlebProgramParser : BTVProgramParser
    {
        private DateTime _day;

        public DateTime Day
        {
            get { return _day; }
            set { _day = value; }
        }

        public BlebProgramParser(String url, BTVChannel c)
        {
            this.Url = url;
            this.Channel = c;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fnfe"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1807:AvoidUnnecessaryStringCreation", MessageId = "stack0")]
        public override void ParseFile()
        {
            String file_name = Path.GetFileNameWithoutExtension(Url);
            String file_year = file_name.Substring(0, 4);
            int int_year = Int32.Parse(file_year);
            String file_month = file_name.Substring(4, 2);
            int int_month = Int32.Parse(file_month);
            String file_day = file_name.Substring(6, 2);
            int int_day = Int32.Parse(file_day);
            _day = new DateTime(int_year, int_month, int_day);
            Console.WriteLine("Parsing file " + Url + " with date " + _day);
            XmlTextReader r = new XmlTextReader(Url);
            BlebProgram tv = new BlebProgram();
            BlebSchedule s = new BlebSchedule();
            String currentTag = "";
            Boolean passed10 = false;
            try
            {
                while ( r.Read() )
                {
                    r.MoveToContent();
                    switch ( r.NodeType )
                    {
                        case XmlNodeType.Element:
                            currentTag = r.Name.ToLower();
                            switch ( currentTag )
                            {
                                case "programme":
                                    //Console.WriteLine("new TVAny_Program");
                                    tv = new BlebProgram();
                                    s = new BlebSchedule();
                                    s.Day = this._day;
                                    s.Channel = Channel.Name;
                                    break;
                            }
                            break;
                        case XmlNodeType.Text:
                        case XmlNodeType.CDATA:
                            switch ( currentTag )
                            {
                                case "desc":
                                    //Console.WriteLine("set Title to " + r.Value);
                                    tv.AddSynopsis("long", r.Value);
                                    break;
                                case "infourl":
                                    //Console.WriteLine("set Synopsis");
                                    tv.AddRelatedInfo("website", r.Value);
                                    break;
                                case "title":
                                    tv.Name = r.Value;
                                    tv.Series = tv.Name;
                                    tv.PID = tv.Name.ToLower() + _day.ToString().ToLower();
                                    s.Pid = tv.PID;
                                    break;
                                case "start":
                                    String hour = r.Value.Substring(0, 2);
                                    int h_int = Int32.Parse(hour);
                                    if ( h_int >= 10 )
                                        passed10 = true;
                                    if (passed10 && (h_int > 0 && h_int < 10))
                                        s.SetNextDay();
                                    s.SetStartTime(r.Value);
                                    break;
                                case "end":
                                    s.SetDuration(r.Value);
                                    break;
                            }
                            break;
                        case XmlNodeType.EndElement:
                            switch ( r.Name.ToLower() )
                            {
                                case "programme":
                                    //Console.WriteLine("end TVAny_Program");
                                    //tv = new TVAny_Program(r.GetAttribute("programId"));
                                    Channel.AddProgram(tv);
                                    Channel.AddSchedule(s);
                                    break;
                            }
                            break;
                    }
                }
            }
            catch 
            {
                /*ignore and try next file */
            }
        }
    }
}